package cn.edu.neu.vmmigeval.cloud.policy;

import cn.edu.neu.vmmigeval.cloud.entity.Cloudlet;
import cn.edu.neu.vmmigeval.cloud.entity.Vm;

import java.util.ArrayList;
import java.util.List;


public class CloudletAllocation {
	
	//随机分配
	public boolean allocateVmForCloudlet(List<Vm> vms, Cloudlet cloudlet){
		if(vms!=null&&vms.size()!=0){
			int nums = vms.size();
			int flag = 0; 
			while(flag < (nums * 3)){
				int random = (int)(Math.random()*nums);
				Vm vm = vms.get(random);
				List<Cloudlet> cls = vm.getCloudlets();
				if(cls!=null&&cls.size()>3){
					//System.out.println("Vm "+vm.getId()+" 已经有"+cls.size()+"个任务了");
				}else if(cls==null||cls.size()==0){
					List<Cloudlet> clss = new ArrayList<Cloudlet>();
					clss.add(cloudlet);
					vm.setCloudlets(clss);
					cloudlet.setVmId(vm.getId());
					return true;
				}else{
					vm.getCloudlets().add(cloudlet);
					cloudlet.setVmId(vm.getId());
					return true;
				}
				flag++;
			}
		}
		return false;
	}
}
